import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.ListAutoNumber;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.EOFException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class Main2 {
    public static void main(String[] args) throws IOException, InvalidFormatException {
        List<String> bihualist = new ArrayList<>();
        List<String> zucilist = new ArrayList<>();
        List<String>  bushoulist = new ArrayList<>();
        // Load the Excel file
        String excelFilePath = "E:\\desktop\\ziku\\bushoubihua.xlsx";
        Workbook workbook = new XSSFWorkbook(new File(excelFilePath));
        Sheet sheet = workbook.getSheetAt(0); // assuming you want the first sheet
        for (Row row : sheet) {
            Cell cell = row.getCell(0); // get the first column
            String keyword = cell.getStringCellValue();

            // Replace the keyword in the URL
            String url = "https://hanyu.baidu.com/s?wd="+keyword+"&ptype=zici#basicmean";
            Document document = Jsoup.parse(new URL(url),30000);
            Element radical = document.getElementById("radical");
            Element stroke_count = document.getElementById("stroke_count");
            Element zuci = document.getElementById("zuci-wrapper");
            //部首
//            Cell outputCell = row.createCell(1);
//            try {
//                Elements span1 = radical.getElementsByTag("span");
//                outputCell.setCellValue(span1.text());
//            }catch (Exception e){
//                outputCell.setCellValue("百度");
//                bushoulist.add(keyword);
//            }





            //总笔画
            Cell outputCell1 = row.createCell(2);

            try{
                Elements bihua = stroke_count.getElementsByTag("span");
                outputCell1.setCellValue(bihua.text());
            }catch (Exception e){
                outputCell1.setCellValue("百度");
                bihualist.add(keyword);
            }






            //组词
//            Cell outputCell2 = row.createCell(3);
//            try {
//                String html = zuci.toString();
//                Document parse = Jsoup.parse(html);
//                Element div = parse.getElementsByTag("div").first();
//                // 遍历所有的"div"元素
//                if (div != null){
//                    Elements div2 = div.getElementsByClass("tab-content");
//                    if (div2.size() > 0) {
//                        // 获取第二个"label"元素
//                        Element secondLabel = div2.get(0);
////                    System.out.println(secondLabel);
//                        // 获取该"label"元素下所有的"span"元素
//                        Elements spans = secondLabel.getElementsByTag("a");
////                    System.out.println(spans);
//                        // 遍历所有的"span"元素并打印其内容
//                        String pinyin = "";
//                        int index = 0;
//                        for (Element span2 : spans) {
//                            pinyin+= span2.text()+"、";
//                            index++;
//                            if(index == 3){
//                                break;
//                            }
//                        }
//                        outputCell2.setCellValue(pinyin);
//                    }
//                }
//            }catch (Exception e){
//                outputCell2.setCellValue("百度");
//                zucilist.add(keyword);
//            }

            // Print a message to the console
            System.out.println("Successfully parsed and wrote keyword: " + keyword);
        }
        System.out.println(bihualist);
        System.out.println(zucilist);
        // Save the changes to a new file
        String outputExcelFilePath = "E:\\desktop\\ziku\\bushoubihua001.xlsx";
        try (FileOutputStream outputStream = new FileOutputStream(outputExcelFilePath)) {
            workbook.write(outputStream);
        }

        workbook.close();
    }
}
